package m.store.service;

import m.common.model.util.ModelQueryCheck;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.common.service.Service;
import m.store.model.*;
import m.system.util.GenerateID;
import m.system.util.ObjectUtil;

import java.util.Date;

public class GoodsRecordNoticeService extends Service {

    /**
     * 插入通知
     */
    public <T extends GoodsRecordModel> void insertNotice(T record,String type) throws Exception {
        if(ObjectUtil.isEmptyOid(record)) return;
        GoodsRecordNotice model= ModelQueryList.getModel(GoodsRecordNotice.class,record.getOid(),"*");
        if(null==model) model=new GoodsRecordNotice();
        if(record instanceof GoodsStockRecord) {
            GoodsStockRecord sr=ModelQueryList.getModel(GoodsStockRecord.class,record.getOid(),"*","stock.*","repository.*");
            model.setRepository(sr.getRepository());
            model.setNoticeUser(sr.getRepository().getManageUser());
            model.setGoods(sr.getGoods());
            model.setPrice(sr.getPrice());
            model.setNum(sr.getStockNum());
            model.setPreferential(sr.getPreferential());
            model.setAmount(sr.getStockAmount());
            model.setPreferentialPrice(sr.getPreferentialPrice());
            model.setRemark(sr.getRemark());
            model.setType(type);
            model.setOrderOid(sr.getStock().getOid());
            model.setOrderNo(sr.getStock().getOrderNo());
            model.setOrderDate(sr.getStock().getStockDate());
            model.setInputUser(sr.getStock().getInputUser());
            model.setGuest(sr.getStock().getGuest());
        }else if(record instanceof GoodsOutRecord){
            GoodsOutRecord or=ModelQueryList.getModel(GoodsOutRecord.class,record.getOid(),"*","sale.*","record.repository.*");
            model.setRepository(or.getRecord().getRepository());
            model.setNoticeUser(or.getRecord().getRepository().getManageUser());
            model.setGoods(or.getGoods());
            model.setPrice(or.getPrice());
            model.setNum(or.getNum());
            model.setPreferential(or.getPreferential());
            model.setAmount(or.getSaleAmount());
            model.setPreferentialPrice(or.getPreferentialPrice());
            model.setRemark(or.getRemark());
            model.setType(type);
            model.setOrderOid(or.getSale().getOid());
            model.setOrderNo(or.getSale().getOrderNo());
            model.setOrderDate(or.getSale().getSaleDate());
            model.setInputUser(or.getSale().getInputUser());
            model.setGuest(or.getSale().getGuest());
        }else{
            return;
        }
        model.setNoticeDate(new Date());
        model.setStatus(GoodsRecordNotice.Status_Notice);
        if(ObjectUtil.isEmptyOid(model)){
            model.setRecordOid(record.getOid());
            model.setOid(GenerateID.generatePrimaryKey());
            ModelUpdateUtil.insertModel(model);
            record.setNotice(model);
            ModelUpdateUtil.updateModel(record,"notice.oid");
        }else{
            ModelUpdateUtil.updateModel(model);
        }
    }

    /**
     * 移除通知标识， 不存在或已阅返回true
     */
    public <T extends GoodsRecordModel> boolean removeNotice(T record) throws Exception {
        if(ObjectUtil.isEmptyOid(record)) return false;
        GoodsRecordNotice model= ModelQueryList.getModel(GoodsRecordNotice.class,new String[]{"*"},
                QueryCondition.eq("recordOid",record.getOid()));
        if(null==model) return true;
        if(GoodsRecordNotice.Status_Read.equals(model.getStatus())) return false;
        model.setStatus(GoodsRecordNotice.Status_Delete);
        model.setOperDate(new Date());
        ModelUpdateUtil.updateModel(model,"status","operDate");
        return true;
    }
    public void readNotice(GoodsRecordNotice model) throws Exception {
        GoodsRecordNotice m=ModelQueryCheck.init(GoodsRecordNotice.class,model.getOid())
                .checkStrEq("status",GoodsRecordNotice.Status_Notice,"状态错误")
                .getModel("oid");
        m.setStatus(GoodsRecordNotice.Status_Read);
        m.setOperDate(new Date());
        ModelUpdateUtil.updateModel(m,"status","operDate");
    }
}
